//
//  HomeViewController.m
//  ZZmetro
//
//  Created by 一介布衣 on 2020/6/15.
//  Copyright © 2020 一介布衣. All rights reserved.
//

#import "HomeViewController.h"
#import "HomeTableCell.h"
#import "HomeTableHeaderCell.h"
#import "SDCycleScrollView.h"
#import "FuncInfo.h"
#import "WebViewController.h"
#import "TakePhotoController.h"
#import "BottomSheet.h"
#import "SingleColumnDataPickerView.h"
#import "Datapicker.h"
#import "ImageCompressTool.h"
#import "InspectionController.h"
#import <STPopup/STPopup.h>

@interface HomeViewController () <UITableViewDelegate, UITableViewDataSource, SDCycleScrollViewDelegate,
                                  RootCellDelegate, UIImagePickerControllerDelegate, UINavigationControllerDelegate>

@property (strong, nonatomic) UITableView* tableView;
//Banner广告
@property(nonatomic,strong) SDCycleScrollView *bannerAd;
//滚动文字广告
@property(nonatomic,strong) SDCycleScrollView *textAd;
@property (nonatomic, strong) NSMutableDictionary *dicH;
@end

@implementation HomeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupView];
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:YES];
}

-(void) setupView {
    [self.view addSubview:self.tableView];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (self.dicH[indexPath]) {
        NSNumber *num = self.dicH[indexPath];
        //NSLog([NSString stringWithFormat:@"===height: %f", [num floatValue]]);
        return [num floatValue];
    } else {
        return 200 + 36;
    }
}

#define mark --UITableViewDelegate
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 2;
}

#pragma mark ====== RootTableCellDelegate ======

- (void)updateTableViewCellHeight:(HomeTableCell *)cell andheight:(CGFloat)height andIndexPath:(NSIndexPath *)indexPath {
    if (![self.dicH[indexPath] isEqualToNumber:@(height)]) {
        self.dicH[indexPath] = @(height);
        //NSLog([NSString stringWithFormat:@"===height: %i", height]);
        [self.tableView reloadData];
    } else {
        //NSLog([NSString stringWithFormat:@"===exist,height: %i", height]);
    }
}

//点击UICollectionViewCell的代理方法
- (void)didSelectItemAtIndexPath:(NSIndexPath *)indexPath withContent:(FuncInfo *)content {
    //UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"" message:content.name delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
    //[alertView show];
    
    
    [self onItemClick:indexPath withContent:content];
    //[self testItemClick:indexPath];
}

-(void) onItemClick:(NSIndexPath *)indexPath withContent:(FuncInfo *)content {
    switch (content.action) {
        case ITEM_ACTION_CLOCK:
            
        break;
            
        case ITEM_ACTION_KPI:
            [AppHelper enterH5Page:urlKpi];
        break;
            
        case ITEM_ACTION_ASK_LEAVE:
            [AppHelper enterH5Page:urlAskLeave];
        break;
            
        case ITEM_ACTION_PERSONAL_PROFILE:
            [AppHelper enterH5Page:urlPersonal];
        break;
            
        //==============
        case ITEM_ACTION_INSPECT:
        {
            UINavigationController* navCtl = [UIViewController currentNavigationController];
            InspectionController *inspectionCtrl = [InspectionController new];
            [navCtl pushViewController:inspectionCtrl animated:NO];
        }
        break;
            
        case ITEM_ACTION_STATION_SCHEDULE:
            [AppHelper enterH5Page:urlStationSchedule];
        break;
            
        case ITEM_ACTION_MY_SCHEDULE:
            [AppHelper enterH5Page:urlMySchedule];
        break;
        
        //==============
        case ITEM_ACTION_FILE_STUDY:
            [AppHelper enterH5Page:urlFileStudy];
        break;
            
        case ITEM_ACTION_MY_METTING:
            [AppHelper enterH5Page:urlMyMeeting];
        break;
            
        case ITEM_ACTION_REWORD_APPLY:
            [AppHelper enterH5Page:urlRewardApply];
        break;
            
        case ITEM_ACTION_TAXATION_FORM:
            [AppHelper enterH5Page:urlTaxtion];
        break;
            
        case ITEM_ACTION_COMMUNITY_PARTY:
            [AppHelper enterH5Page:urlCommnistParty];
        break;
            
        //================
        case ITEM_ACTION_PENDING_TASK:
            [AppHelper enterH5Page:urlPendingTask];
        break;
            
        case ITEM_ACTION_MSG_NOTI:
            [AppHelper enterH5Page:urlMsgNotfiy];
        break;
            
        case ITEM_ACTION_SETTING:
        
        break;
        default:
            break;
    }
}

/**
 * item click test
 */
-(void) testItemClick:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        UINavigationController *navCtl = [UIViewController currentNavigationController];
        TakePhotoController *takeCtrl = [TakePhotoController new];
        takeCtrl.pickType = 2;
        [navCtl pushViewController:takeCtrl animated:NO];
    } else if (indexPath.section == 1) {
        NSArray* data = @[@"测试1",@"测试2",@"测试3",@"测试4",@"测试5"];
        [Datapicker pickData:self srcData:data callback:nil];
    } else if (indexPath.section == 3) {
        if (indexPath.row == 0) {
            [Datapicker pickYearMonthDayHourMinute:self];
        } else if (indexPath.row == 1) {
            [Datapicker pickYearMonthDay:self];
        } else if (indexPath.row == 2) {
            [Datapicker pickHourMinute:self];
        } else if (indexPath.row == 3) {
            [self compressImg];
        }
    } else {
        UINavigationController *navCtl = [UIViewController currentNavigationController];
        WebViewController *web = [WebViewController new];
        //web.url = @"https://www.baidu.com/";
        NSString* url = [AppHelper formatUrl:urlAskLeave];
        web.url = url;
        [navCtl pushViewController:web animated:YES];
    }
}

-(void) compressImg {
    UIImage *img = [UIImage imageNamed:@"be_welcome.jpeg"];
    CGSize size = CGSizeMake(SCREEN_WIDTH, SCREEN_HEIGHT);
    [ImageCompressTool CompressToDataAtBackgroundWithImage:img
                                                    ShowSize:size
                                                    FileSize:100
                                                       block:^(NSData *resultData) {
        UIImage *newIMG = [UIImage imageWithData:resultData];
        NSData *newIMGData = UIImageJPEGRepresentation(newIMG,1);
        //获取主线程
        dispatch_async(dispatch_get_main_queue(), ^{
            NSString *info = [NSString stringWithFormat:@"压缩得到的NSData大小：%lu kb\n\n使用Data再次创建的UIImage的大小： %lu kb",[resultData length]/1024,[newIMGData length]/1024];
            [Toast toast:info];
        });
    }];
}

#pragma mark ====== init ======
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.frame style:UITableViewStylePlain];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellEditingStyleNone; //隐藏分割线
        
        [_tableView registerClass:[HomeTableHeaderCell class] forCellReuseIdentifier:@"headertablecell"];
        [_tableView registerClass:[HomeTableCell class] forCellReuseIdentifier:@"hometablecell"];
    }
    return _tableView;
}

- (NSMutableDictionary *)dicH {
    if (!_dicH) {
        _dicH = [[NSMutableDictionary alloc] init];
    }
    return _dicH;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.row == 0) {
        HomeTableHeaderCell *headerCell = (HomeTableHeaderCell*) [tableView dequeueReusableCellWithIdentifier:@"headertablecell"];
        return headerCell;
    }
    
    HomeTableCell *cell = (HomeTableCell*) [tableView dequeueReusableCellWithIdentifier:@"hometablecell"];
    cell.delegate = self;
    cell.indexPath = indexPath;
    return cell;
}

@end
